#include "mainwindow.h"
#include <QTimeLine>
#include <QRandomGenerator>
#include <QGraphicsItemAnimation>
#include "flashitem.h"
#include "startitem.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    createActions();
    createMenus();
    scene= new QGraphicsScene;
    scene->setSceneRect(-200,-200,400,400);
    initScene(); //初始化场景
    QGraphicsView *view= new QGraphicsView;
    view->setScene(scene);
    view->setMinimumSize(400,400);
    view->show();
    setCentralWidget(view);
    resize(550,450);
    setWindowTitle(tr("Graphics Items"));
}

MainWindow::~MainWindow()
{
}

void MainWindow::initScene()
{
    int i;
    for(i=0;i<3;i++)
        slotAddEllipseItem();
    for(i=0;i<3;i++)
        slotAddPolygonItem();
    for(i=0;i<3;i++)
        slotAddTextItem();
    for(i=0;i<3;i++)
        slotAddRectItem();
    for(i=0;i<3;i++)
        slotAddAlphaItem();
    for(i=0;i<3;i++)
        slotAddFlashItem();
    for(i=0;i<3;i++)
        slotAddAnimationItem();
}

void MainWindow::createActions()
{
    newAct = new QAction(tr("新建"),this);
    clearAct = new QAction(tr("清除") , this) ;
    exitAct = new QAction(tr("退出"),this);
    addEllipseItemAct = new QAction(tr("加入椭圆"),this);
    addPolygonItemAct= new QAction(tr("加入多边形"),this);
    addTextItemAct = new QAction(tr("加入文字"),this);
    addRectItemAct = new QAction(tr("加入长方形"),this);
    addAlphaItemAct= new QAction(tr("加入透明图片"),this);
    addFlashItemAct = new QAction(tr("加入闪烁圆"),this);
    addAnimItemAct = new QAction(tr(" 加入星星"), this);

    connect(newAct,&QAction::triggered,this,&MainWindow::slotNew);
    connect(clearAct,&QAction::triggered,this,&MainWindow::slotClear);
    connect(exitAct,&QAction::triggered,this,&MainWindow::close);
    connect(addEllipseItemAct,&QAction::triggered,this,&MainWindow::slotAddEllipseItem);
    connect(addPolygonItemAct,&QAction::triggered,this,&MainWindow::slotAddPolygonItem);
    connect(addTextItemAct,&QAction::triggered,this,&MainWindow::slotAddTextItem);
    connect(addRectItemAct,&QAction::triggered,this,&MainWindow::slotAddRectItem);
    connect(addAlphaItemAct,&QAction::triggered,this,&MainWindow::slotAddAlphaItem);
    connect(addFlashItemAct,&QAction::triggered,this,&MainWindow::slotAddFlashItem);
    connect(addAnimItemAct,&QAction::triggered,this,&MainWindow::slotAddAnimationItem);
}

void MainWindow::createMenus()
{
    QMenu *fileMenu = menuBar()->addMenu(tr("文件")) ;
    fileMenu->addAction(newAct);
    fileMenu->addAction(clearAct);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAct);
    QMenu *itemsMenu = menuBar()->addMenu(tr("元素")) ;
    itemsMenu->addAction(addEllipseItemAct);
    itemsMenu->addAction(addPolygonItemAct);
    itemsMenu->addAction(addTextItemAct);
    itemsMenu->addAction(addRectItemAct);
    itemsMenu->addAction(addAlphaItemAct);
    itemsMenu->addAction(addAnimItemAct);
}

void MainWindow::slotNew()
{
    slotClear();
    initScene();
    MainWindow *newWin = new MainWindow;
    newWin->show();
}

void MainWindow::slotClear()
{
    QList<QGraphicsItem*> listItem = scene->items();
    while (! listItem. empty())
    {
        scene->removeItem(listItem.at (0));
        listItem.removeAt(0);
    }

}

void MainWindow::slotAddEllipseItem()
{
    QGraphicsEllipseItem *item= new QGraphicsEllipseItem(QRectF(0,0,80, 60));
    item->setPen (Qt::NoPen);
    item->setBrush(QColor(QRandomGenerator::global()->bounded(0,256),QRandomGenerator::global()->bounded(0,256),QRandomGenerator::global()->bounded(0,256)));
    item->setFlag(QGraphicsItem::ItemIsMovable);
    scene->addItem(item);
    item->setPos(QRandomGenerator::global()->bounded(0,(int)scene->sceneRect().width())-200,
                 QRandomGenerator::global()->bounded(0,(int)scene->sceneRect().height())-200);
}

void MainWindow::slotAddPolygonItem()
{
    QVector<QPoint> v;
    v<<QPoint(30,-15)<<QPoint(0,-30)<<QPoint(-30,-15)
    <<QPoint(-30,15)<<QPoint(0,30)<<QPoint(30,15);
    QGraphicsPolygonItem *item= new QGraphicsPolygonItem(QPolygonF(v));
    item->setBrush(QColor(QRandomGenerator::global()->bounded(0,256),QRandomGenerator::global()->bounded(0,256),QRandomGenerator::global()->bounded(0,256)));
    item->setFlag(QGraphicsItem::ItemIsMovable);
    scene->addItem(item);
    item->setPos(QRandomGenerator::global()->bounded(0,(int)scene->sceneRect().width())-200,
                 QRandomGenerator::global()->bounded(0,(int)scene->sceneRect().height())-200);
}

void MainWindow::slotAddTextItem()
{
    QFont font("Times",16);
    QGraphicsTextItem *item= new QGraphicsTextItem("Hello Qt");
    item->setFont(font);
    item->setFlag(QGraphicsItem::ItemIsMovable);
    item->setDefaultTextColor(QColor(QRandomGenerator::global()->bounded(0,256),QRandomGenerator::global()->bounded(0,256),QRandomGenerator::global()->bounded(0,256)));
    scene->addItem(item);
    item->setPos(QRandomGenerator::global()->bounded(0,(int)scene->sceneRect().width())-200,
                 QRandomGenerator::global()->bounded(0,(int)scene->sceneRect().height())-200);
}

void MainWindow::slotAddRectItem()
{
    QGraphicsRectItem *item= new QGraphicsRectItem(QRectF(0,0,60,60));
    QPen pen;
    pen.setWidth(3);
    pen.setColor(QColor(QRandomGenerator::global()->bounded(0,256),QRandomGenerator::global()->bounded(0,256),QRandomGenerator::global()->bounded(0,256)));
    item->setPen(pen);
    item->setBrush(QColor(QRandomGenerator::global()->bounded(0,256),QRandomGenerator::global()->bounded(0,256),QRandomGenerator::global()->bounded(0,256)));
    item->setFlag(QGraphicsItem::ItemIsMovable);
    scene->addItem(item);
    item->setPos(QRandomGenerator::global()->bounded(0,(int)scene->sceneRect().width())-200,
                 QRandomGenerator::global()->bounded(0,(int)scene->sceneRect().height())-200);
}

void MainWindow::slotAddAlphaItem()
{
    QGraphicsPixmapItem *item= scene->addPixmap(QPixmap("image.png"));
    item->setFlag(QGraphicsItem::ItemIsMovable);
    item->setPos(QRandomGenerator::global()->bounded(0,(int)scene->sceneRect().width())-200,
                 QRandomGenerator::global()->bounded(0,(int)scene->sceneRect().height())-200);
}

void MainWindow::slotAddFlashItem()
{
    FlashItem *item = new FlashItem;
    scene->addItem(item);
    item->setPos(QRandomGenerator::global()->bounded(0,(int)scene->sceneRect().width())-200,
                 QRandomGenerator::global()->bounded(0,(int)scene->sceneRect().height())-200);
}

void MainWindow::slotAddAnimationItem()
{
    StartItem *item= new StartItem;
    QGraphicsItemAnimation *anim = new QGraphicsItemAnimation;
    anim->setItem(item);
    QTimeLine *timeLine = new QTimeLine(4000);
    timeLine->setEasingCurve(QEasingCurve::SineCurve);
    timeLine->setLoopCount(0);
    anim->setTimeLine(timeLine);
    int y =QRandomGenerator::global()->bounded(0,400)-200;
    for(int i=0;i<400;i++)
        anim->setPosAt(i/400.0,QPointF(i-200,y));
    timeLine->start();
    scene->addItem(item);
}

